package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.ConsumerRelateQO;
import com.xingkeduo.pojo.table.ConsumerRelateTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/2/18 18:29
 **/
@Service
public class ConsumerRelateService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(ConsumerRelateTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<ConsumerRelateTO, ConsumerRelateQO> dao;


    /**
     * 总账户分页列表
     *
     * @return : List<ConsumerTO>
     * @author : gaozifeng
     * @date : 2019年2月26日20:29:33
     */
    public List<ConsumerRelateTO> selectConsumerRelateList(ConsumerRelateQO consumerRelateQO) throws Throwable {
        return dao.select(NAMESPACE, consumerRelateQO);
    }

    /**
     * 总账户id集合
     *
     * @return : List<Long>
     * @author : gaozifeng
     * @date : 2019年2月26日20:29:33
     */
    public List<Long> selectConsumerListForid(ConsumerRelateQO consumerRelateQO) throws Throwable {
        return dao.selectIndex(NAMESPACE, consumerRelateQO);
    }

    /**
     * 新增
     *
     * @param consumerRelateTO:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/2/19 11:27
     */
    public void insert(ConsumerRelateTO consumerRelateTO) throws Throwable {
        consumerRelateTO.initId(IdentityUtil.nextId(Worker.USER));
        consumerRelateTO.setUpdatedBy(consumerRelateTO.getId());
        consumerRelateTO.setCreatedBy(consumerRelateTO.getId());
        if (dao.insert(NAMESPACE, consumerRelateTO) < 1) {
            throw new DataException(DataException.Type.FAIL, "新增账户信息");
        }
    }

    /**
     * 新增批量
     *
     * @param list:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/2/19 11:27
     */
    public void insertList(List<ConsumerRelateTO> list) throws Throwable {
        if (dao.insert(NAMESPACE, list) < list.size()) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }

    /**
     * 修改
     *
     * @param consumerRelateTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/27 10:21
     */
    public void update(ConsumerRelateTO consumerRelateTO) throws Throwable {
        if (dao.update(NAMESPACE, consumerRelateTO) < 1)
            throw new DataException(DataException.Type.FAIL, "修改账户信息");
    }

    /** 批量修改
     * @param fileTOS:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/5/6 14:57
     */
    @Transactional
    public int updateList(List<ConsumerRelateTO> fileTOS) throws Throwable {
        int count = 0;
        for (Iterator<ConsumerRelateTO> iterator = fileTOS.iterator(); iterator.hasNext(); ) {
            count += dao.update(NAMESPACE, iterator.next());
        }
        return count;
    }

}
